#ifndef ROBOTITEM_H
#define ROBOTITEM_H

#include <QObject>
#include <QGraphicsItem>
#include <QColor>
#include <QMetaType>

#include <iostream>
#include <iomanip>
#include <cmath>
/*
* @Brief: 小车状态数据表
*/
enum RobotRunningState {
    Ready = 0,
    Waiting,
    Pickup,
    Delivery,
    Back
};
/**
* @brief: 小车基础运动数据类型
*/
class RobotBaseData {
public:
    RobotBaseData() {};
//    RobotBaseData(const RobotBaseData&){};
    ~RobotBaseData(){};

    int num = 0;
    int net = 0;
    int x = 0;
    int y = 0;
    double theta = 0.0;
    double p = 0.0;
    double v = 0.0;
    double wv = 0.0;
    double ele = 0.0;
    RobotRunningState state;
    double load = 0.0;
    double a = 0.0;
    double b = 0.0;
    //重载输出
    friend std::ostream &operator << (std::ostream& os, const RobotBaseData& data);

};

Q_DECLARE_METATYPE(RobotBaseData);

/**
* @brief  小车基础网络数据类型
*/
struct RobotNetData {
    QString ip = "";     //小车IP
    QString port = "";   //小车端口
    int delay = 0;      //网络延迟
    bool isConnect = false; //是否连接
};
/**
* @brief: 机器人Item
*/
class RobotItem : public QObject, public QGraphicsItem
{
    Q_OBJECT
public:
    RobotItem(int id, QRectF rect, int x, int y, QGraphicsItem* parent = nullptr);
    RobotItem(QRectF rect, const RobotBaseData& data, QGraphicsItem* parent = nullptr);
    ~RobotItem();
    //override
    QRectF boundingRect() const override;
    QPainterPath shape() const override;
    void paint(QPainter *painter,
               const QStyleOptionGraphicsItem *option,
               QWidget *widget) override;
    /**
    * @brief  获取positon
    */
    QPoint getCurPos() {return QPoint(m_data.x, m_data.y);}
    /**
    * @brief: 设置终点位置
    */
    void setEndPos(const QPoint& point);
    /**
    * @brief: 设置终点位置和角度
    */
    void setEndPos(const QPoint& point, double theta);
    /**
    * @brief: 返回机器人数据
    */
    RobotBaseData getRobotData() {return m_data;}
    /**
    * @brief: 根据RobotBaseData设置机器人状态
    */
    void setRobotData(const RobotBaseData& data);
    /**
    * @brief  获取当前itemCount
    */
    static int getItemCount() {return m_itemCount;}
    void setColor(const QColor& color);
    QColor getColor() {return m_color;}

protected:
    void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
    void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
    void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;

private:
    //记录多少个Item
    static int m_itemCount;

    RobotBaseData m_data;   //机器人数据
    QColor m_color = QColor(133, 193, 233);
    QRect m_itemRect = QRect(-20, -20, 40, 40);
    QPoint m_coordinateOffset;

signals:
    void dataChanged(const RobotBaseData&); //数据发生改变
};

#endif // ROBOTITEM_H
